/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cas.dbaccess;

import com.cas.hibernate.entity.Appointment;
import com.cas.hibernate.entity.CasUser;
import com.cas.hibernate.entity.Country;
import com.cas.hibernate.entity.Doctor;
import com.cas.hibernate.entity.Patient;
import com.cas.hibernate.util.HibernateUtil;
import com.cas.util.BCrypt;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


/**
 *
 * @author suraj
 */
public class CASDBAccessImpl implements CASDBAccess {

    public CASDBAccessImpl() {
    }

    @Override
    public List<Doctor> getAllDoctors() {
        List<Doctor> list = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Doctor as doctor");
            list = (List<Doctor>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Doctor getDoctor(String doctorId) {
        List<Doctor> list = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Doctor as doctor where doctor.id = " + doctorId);
            list = (List<Doctor>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    @Override
    public String saveDoctor(Doctor doctor) {

        if (doctor == null) {
            return null;
        }
        System.out.println("............." + doctor.getFirstName());
        Transaction tx = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            tx = session.beginTransaction();
            session.update(doctor);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        return Long.toString(doctor.getId());
    }

    @Override
    public String createDoctor(Doctor doctor) {
        if (doctor == null) {
            return null;
        }
        Transaction tx = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            tx = session.beginTransaction();
            session.save(doctor);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        System.out.println("****************" + doctor.getId());
        return Long.toString(doctor.getId());
    }

    @Override
    public void deleteDoctor(long doctorId) {
        Transaction tx = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            tx = session.beginTransaction();
            Object d = session.get(Doctor.class, doctorId);
            if (d != null) {
                session.delete(d);
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }

    @Override
    public List<Patient> getAllPatients() {
        List<Patient> list = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Patient as patient");
            list = (List<Patient>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Patient getPatient(String patientId) {
        List<Patient> list = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Patient as patient where patient.id = " + patientId);
            list = (List<Patient>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list == null || list.size() == 0 ? null : list.get(0);
    }
    
    @Override
    public String savePatient(Patient patient) {

        if (patient == null) {
            return null;
        }
        System.out.println("............." + patient.getFirstName());
        Transaction tx = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            tx = session.beginTransaction();
            session.update(patient);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        //return null;
        return Long.toString(patient.getId());
    }

    @Override
    public String createPatient(Patient patient) {
        if (patient == null) {
            return null;
        }
        Transaction tx = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            tx = session.beginTransaction();
            session.save(patient);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        System.out.println("****************" + patient.getId());
        //return null;
        return Long.toString(patient.getId());
    }

    @Override
    public void deletePatient(long patientId) {
        Transaction tx = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            tx = session.beginTransaction();
            Object d = session.get(Patient.class, patientId);
            if (d != null) {
                session.delete(d);
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }

    @Override
    public List<Appointment> getAllAppointments() {
        List<Appointment> list = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Appointment as appointment");
            list = (List<Appointment>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Appointment> getAppointmentsForDoctor(Doctor doctor) {
        List<Appointment> list = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Appointment as appointment where appointment.doctorId = " + doctor.getId());
            list = (List<Appointment>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public CasUser validateUser(CasUser user) {
        List<CasUser> list = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from CasUser as user where user.userId = '" + user.getUserId() + "'");
            list = (List<CasUser>) q.list();

            if (list == null || list.size() <= 0) {
                return null;
            }

            CasUser dbUser = list.get(0);
            String dbPwd = dbUser.getPassword();

            if (dbPwd == null || !BCrypt.checkpw(user.getPassword(), dbPwd)) {
                return null;
            }

            return dbUser;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    
    @Override
    public ArrayList<Country> getAllCountries() {
        ArrayList<Country> list = null;
        try {
            Session session = HibernateUtil.getCurrentSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Country as country");
            list = (ArrayList<Country>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
